<template>
  <div class="login-container">
    <van-nav-bar title="黑马头条-登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        required
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写正确手机号',trigger:'onChange',pattern:/^1[3-9]\d{9}$/ }]"
      />
      <van-field
        v-model="formLogin.code"
        type="password"
        required
        label="密码"
        placeholder="登录密码"
        :rules="[{ required: true, message: '请填写登录密码',trigger:'onChange',pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" :loading='isLoading' :disabled='isLoading'  loading-text="拼命登录中..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginApi } from '@/api'
import { Notify } from 'vant'
import { mapMutations } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      isLoading: false,
      formLogin: {
        mobile: '13888888888',
        code: '246810'// 默认只能用246810,后台规定了
      }
    }
  },
  methods: {
    ...mapMutations(['setToken', 'setRefreshToken']),
    async onSubmit () {
      this.isLoading = true
      try {
        const res = await loginApi(this.formLogin)
        console.log(res)
        this.setToken(res.data.data.token)
        this.setRefreshToken(res.data.data.refresh_token)
        this.isLoading = false
        Notify({ type: 'success', message: '成功登录进入啦' })
      } catch (err) {
        this.isLoading = false
        Notify({ type: 'warning', message: '这次不行哦,你再试试' })
      }
    }
  }
}
</script>

<style lang="less" scoped>
//  .van-nav-bar{
//    background-color: #007bff;
//  }
//  /deep/ .van-nav-bar__title{
//    color: white;
//  }
</style>
